<?php if (! defined ( 'BASEPATH' )) exit ( 'No direct script access allowed' );

class Product extends CI_Model {

    function __construct() {
        parent::__construct();
    }
    
    function get_model_pictures($mid){
    	$sql = "select name,cover,path from model_picture where m_id = " . $mid;
    	$query = $this->db->query($sql);
    	return $query->result_object();
    }
    function get_model_comments($mid){
    	$sql = "select mc.id,mc.comment, mc.comment_time, mc.comment_by,mc.img, u.name from model_comment mc,user u where u.uid = mc.comment_by and mc.m_id = " . $mid;
    	$sql .= " order by  mc.comment_time desc";
    	$query = $this->db->query($sql);
    	return $query->result_object();
    }
    function get_model_keys($mid){
    	$sql = "select * from model_keys where m_id = " . $mid;
    	$query = $this->db->query($sql);
    	return $query->result_object();
    }
    function get_model_material($mid){
    	$sql = "select
				mm.material_id as id,
				m.name,
				m.price,
				m.img
				from model_material mm, material m
				where mm.enable = 1
				and m.enable = 1
				and m.id = material_id
				and  mm.m_id = " . $mid;
    	$query = $this->db->query($sql);
    	return $query->result_object();
    }
    function get_model_types(){
    	$sql = "select id , name from model_type where valid = 1";
    	$query = $this->db->query($sql);
    	return $query->result_object();
    }
    function get_model_info($mid){
    	$sql = "select u.uid,
					u.name as username,
				    mi.id as mid,
				    mi.name as mname,
					mi.describes as describes,
					mi.m_type,
					mi.file,
				    mi.r_time,
					mp.price as price,
					(select ifnull(sum(rating)/count(m_id),0) as rating from model_rating where m_id = mi.id) as rating,
					(select ifnull(count(m_id),0) as rating from model_rating where m_id = mi.id) as rating_num,
					(select CONCAT(path , name) from model_picture where m_id = mi.id and cover = 1) as picture
				from user_model mi,
					model_price mp,
					user u
				where mi.uid = u.uid and mi.id = mp.m_id and mp.m_id = " . $mid;
    	$query = $this->db->query($sql);
    	return $query->first_row();
    }
    function get_more_designs($mid){
    	$sql = "SELECT mp.m_id,um.uid,um.name as mname,mp.name as iname,mp.path from user_model um, model_picture mp
				where um.id = mp.m_id and cover = 1
				and uid in (select uid from user_model where id = " . $mid .") LIMIT 0,4";
    	$query = $this->db->query($sql);
    	return $query->result_object();
    }
    function get_more_favorited($mid){
    	$sql = "select uf.uid, uf.mid, u.name from user_favorites uf, user u where uf.uid = u.uid and mid = " . $mid;
    	$query = $this->db->query($sql);
    	return $query->result_object();
    }
    function is_wishlist($uid, $mid){
    	$sql = "select count(1) as count from user_wishlist where uid = ". $uid ." and mid = " . $mid;
    	$query = $this->db->query($sql);
    	$row = $query->first_row();
    	return $row->count > 0;
    }
    function is_favorites($uid, $mid){
    	$sql = "select count(1) from user_favorites where uid = ". $uid ." and mid = " . $mid;
    	$query = $this->db->query($sql);
    	$row = $query->first_row();
    	return $row->count > 0;
    }
    function get_featured_products($type){
    	$sql = "select u.uid,
				u.name as username,
			    mi.id as mid,
			    mi.name as mname,
				mi.describes as describes,
				mp.price as price,
				(select ifnull(sum(rating)/count(m_id),0) as rating from model_rating where m_id = mi.id) as rating,
				(select ifnull(count(m_id),0) as rating from model_rating where m_id = mi.id) as rating_num,
				(select CONCAT(path , name) from model_picture where m_id = mi.id and cover = 1) as picture
			from user_model mi,
				model_price mp,
				user u
			where mi.uid = u.uid and mi.id = mp.m_id LIMIT 0, 8";
    	$query = $this->db->query($sql);
    	return $query->result_object();
    }
    function get_producte_materials($mid){
    	$sql = "select mm.m_id as mid,
		       mm.material_id,
		       m.name as material_name,
		       m.img,
		       m.price
		from
		model_material mm ,material m
		where mm.material_id = m.id
		and m.enable = 1
		and mm.m_id = " . $mid;
    	$query = $this->db->query($sql);
    	return $query->result_object();
    }
    function get_designer_producte($uid){
    	$sql = "select u.uid,
			u.name as username,
		    mi.id as mid,
		    mi.name as mname,
			mi.describes as describes,
			mp.price as price,
			(select ifnull(sum(rating)/count(m_id),0) as rating from model_rating where m_id = mi.id) as rating,
			(select ifnull(count(m_id),0) as rating from model_rating where m_id = mi.id) as rating_num,
			(select CONCAT(path , name) from model_picture where m_id = mi.id and cover = 1) as picture
		from user_model mi,
			model_price mp,
			user u
		where mi.uid = u.uid
		and mi.id = mp.m_id
		and mi.uid = " . $uid . "  ";
    	$sort = $_REQUEST['sort'];
    	if('price' == $sort){
    		$sql .= " order by mp.price desc ";
    	}elseif ('pricelh' == $sort){
    		$sql .= " order by mp.price ";
    	}elseif ('newest' == $sort) {
    		$sql .= " order by mi.r_time ";
    	}
    	$query = $this->db->query($sql);
    	return $query->result_object();
    }
    function get_products($type){
    	$sql = "select u.uid,
			u.name as username,
		    mi.id as mid,
		    mi.name as mname,
			mi.describes as describes,
			mp.price as price,
			(select ifnull(sum(rating)/count(m_id),0) as rating from model_rating where m_id = mi.id) as rating,
			(select ifnull(count(m_id),0) as rating from model_rating where m_id = mi.id) as rating_num,
			(select CONCAT(path , name) from model_picture where m_id = mi.id and cover = 1) as picture
		from user_model mi,
			model_price mp,
			user u
		where mi.uid = u.uid and mi.id = mp.m_id ";
    	$sort = $_REQUEST['sort'];
    	if('price' == $sort){
    		$sql .= " order by mp.price desc ";
    	}elseif ('pricelh' == $sort){
    		$sql .= " order by mp.price ";
    	}elseif ('newest' == $sort) {
    		$sql .= " order by mi.r_time ";
    	}
    	$sql .= " LIMIT 0, 40";
    	$query = $this->db->query($sql);
    	return $query->result_object();
    }
    function get_top_products(){
    	$sql = "select u.uid,
				u.name as username,
			    mi.id as mid,
			    mi.name as mname,
				mi.describes as describes,
				mp.price as price,
				(select ifnull(sum(rating)/count(m_id),0) as rating from model_rating where m_id = mi.id) as rating,
				(select ifnull(count(m_id),0) as rating from model_rating where m_id = mi.id) as rating_num,
				(select CONCAT(path , name) from model_picture where m_id = mi.id and cover = 1) as picture
			from user_model mi,
				model_price mp,
				user u
			where mi.uid = u.uid and mi.id = mp.m_id limit 0,20";
    	$query = $this->db->query($sql);
    	return $query->result_object();
    }
    function update_user_model($arry){
    	$this->db->trans_begin();
    	
    	$tag = date('YmdHis');
    	$file = "" . $arry['uid'] . "_" . $tag . "_" . $arry['name'];
    	
    	$data = array(
    			'uid' => $arry['uid'] ,
    			'name' => $arry['filename'] ,
    			'm_type' => '1' ,
    			'file' => '/model/' . $file
    	);
    	$this->db->insert('user_model', $data);
    	if ($this->db->trans_status () === FALSE) {
			$this->db->trans_rollback ();
			return 0;
		} else {
			$this->db->trans_commit ();
			return self::save_user_model($arry,$file);
		}
    }
    function save_user_model($arry,$file){
    	try {
    		$tempFile = $arry['tmp_name'];
    		$targetFile = $arry['model_dir'] .'/' . $file;
    		move_uploaded_file($tempFile, ($targetFile));
    		return 1;
    	}catch(Exception $e) {
    		return 0;
    	}
    }
}